package com.tanhua.admin.mapper;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.model.db.Log;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface LogMapper extends BaseMapper<Log> {
    /**
     *  统计今日新增用户数
     * @param today
     * @return
     */
    @Select("select count(1) from tb_log where log_time=#{today} and type='0101'")
    Integer countNumRegisteredToday(String today);

    /**
     * 活跃用户数
     * @param today
     * @return
     */
    @Select("select count(distinct user_id) from tb_log where log_time=#{today}")
    Integer countNumActiveByDate(String today);

    /**
     * 登陆次数
     * @param today
     * @return
     */
    @Select("select count(distinct user_id) from tb_log where log_time=#{today} and type in ('0101','0102')")
    Integer countNumLoginByDate(String today);

    /**
     * 次日留存用户数
     * @param today
     * @param yesterday
     * @return
     */
    @Select("select count(distinct user_id) from tb_log where log_time=#{yesterday} and type='0101' and user_id in (" +
            " select distinct user_id from tb_log where log_time=#{today})")
    Integer countNumRetention1d(@Param("today") String today, @Param("yesterday") String yesterday);

    /**
     * 查询用户活跃数
     * @param date
     * @return
     */
    @Select("select COUNT(distinct user_id) from tb_log where log_time > #{date}")
    Integer countNumActiveByBetweenDate(DateTime date);
}

